﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;

public partial class UserControls_UserSelection : System.Web.UI.UserControl
{
  public static int GroupId;

  public static object UserId;
  
  DataTable dt;
  //public static ArrayList arr = new ArrayList();

  private void LoadGroup()
  {
      ddlGroup.Items.Clear();
      dt = new DataTable();
      dt = DALTask.GetGroup();
      ddlGroup.Items.Add("--Select One--");
      ddlGroup.SelectedIndex = ddlGroup.Items.IndexOf(ddlGroup.Items.FindByText("--Select One--"));
      ListItem list=new ListItem();
      list.Text = "All";
      list.Value = "0";
      ddlGroup.Items.Add(list);
      for (int i = 0; i < dt.Rows.Count; i++)
      {
          ListItem li = new ListItem();
          li.Text = dt.Rows[i][1].ToString();
          li.Value = dt.Rows[i][0].ToString();
          ddlGroup.Items.Add(li);
      }
     
  }

  private void LoadUser(int groupId)
  {
      ddlUser.Items.Clear();
      dt = new DataTable();
      dt = DALTask.GetUser(groupId);
      ddlUser.Items.Add("--Select One--");
      ddlUser.SelectedIndex = ddlUser.Items.IndexOf(ddlUser.Items.FindByText("--Select One--"));
      ListItem list = new ListItem();
      list.Text = "All";
      list.Value = "0";
      ddlUser.Items.Add(list);
      for (int i = 0; i < dt.Rows.Count; i++)
      {
          ListItem li = new ListItem();
          li.Text = dt.Rows[i][1].ToString();
          li.Value = dt.Rows[i][0].ToString();
          ddlUser.Items.Add(li);
      }
  }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UserSelection.sObj.Clear();
            LoadGroup();
        }
      
    }

     protected void Insert()
     {

         GroupId = Convert.ToInt32(ddlGroup.SelectedValue);
         UserId = ddlUser.SelectedValue;
     }

   public void clear()
   {

       //UserControls_UserSelection obj = new UserControls_UserSelection();
       ddlGroup.SelectedIndex = -1;
       ddlUser.SelectedIndex = -1;
   }



   protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
   {
       if (ddlGroup.SelectedItem.Text == "--Select One--")
       {
           lblMessage.Text = "Please Select User...";
       }
       else
       {
           LoadUser(Convert.ToInt32(ddlGroup.SelectedItem.Value));
       }
   }
   protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
   {
       if (ddlUser.SelectedItem.Text == "--Select One--")
       {
           lblMessage.Text = "Please Select User...";
       }
   }

   #region Old Code
   //private bool isPresent(int value)   //return false if already present in container
   //{
   //    bool flag = true;
   //    for (int i = 0; i < arr.Count; i++)
   //    {
   //        if (Convert.ToInt32(arr[i]) == value)
   //        {
   //            flag = false;
   //            break;
   //        }
   //    }
   //    return flag;
   //}
   #endregion

   protected void btnSelect_Click(object sender, EventArgs e)
   {
       StudentInfo user;
       if (ddlGroup.SelectedItem.Text != "--Select One--" && ddlUser.SelectedItem.Text != "--Select One--")
       {
           DataTable dt = new DataTable();
          
           if (ddlGroup.SelectedItem.Text == "All" && ddlUser.SelectedItem.Text == "All")
           {
               dt = DALTask.GetAllUsers(Int32.Parse(ddlGroup.SelectedItem.Value), ddlUser.SelectedItem.Value);

               
               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   if (!IsUserSelected(dt.Rows[i][0].ToString()))
                   {
                       user = new StudentInfo(dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString());
                     
                       UserSelection.sObj.Add(user);
                       lblMessage.Text = "Total User Selected: " + UserSelection.sObj.Count.ToString();
                       
                   }
               }

           }
           if (ddlGroup.SelectedItem.Text == "All" && ddlUser.SelectedItem.Text != "All")
           {
               if (!IsUserSelected(ddlUser.SelectedItem.Value))
               {

                   StudentInfo User2 = new StudentInfo(ddlUser.SelectedItem.Value, ddlUser.SelectedItem.Text);

                   UserSelection.sObj.Add(User2);
                   lblMessage.Text = "Total User Selected: " + UserSelection.sObj.Count.ToString();

               }

           }

           if (ddlGroup.SelectedItem.Text != "All" && ddlUser.SelectedItem.Text == "All")
           {
               dt = DALTask.GetAllUsers(Int32.Parse(ddlGroup.SelectedItem.Value), ddlUser.SelectedItem.Value);

              
               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   if (!IsUserSelected(dt.Rows[i][0].ToString()))
                   {
                       user = new StudentInfo(dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString());
                      
                       UserSelection.sObj.Add(user);
                       lblMessage.Text = "Total User Selected: " + UserSelection.sObj.Count.ToString();
                      
                   }
               }
           }
          

           //check whether user already selected or not and if not selected already add it now

           if (ddlGroup.SelectedItem.Text != "All" && ddlUser.SelectedItem.Text != "All")
           {
              
               if (!IsUserSelected(ddlUser.SelectedItem.Value))
               {
                   
                   StudentInfo User2 = new StudentInfo(ddlUser.SelectedItem.Value, ddlUser.SelectedItem.Text);
                  
                   UserSelection.sObj.Add(User2);
                   lblMessage.Text = "Total User Selected: " + UserSelection.sObj.Count.ToString();
                  
               }
           }
       }
       else
       {
           lblMessage.ForeColor = Color.Red;
           lblMessage.Text = "Please Select Group and User...";
       }
       //clear();
   }

   public bool IsUserSelected(object userid)
   {
       bool isSelected = false;

       for (int i = 0; i < UserSelection.sObj.Count; i++)
       {
           if (UserSelection.sObj[i].sid == userid)
           {
               isSelected = true;
           }
       }

       //foreach (StudentInfo User in UserSelection.sObj)
       //{
       //    if (User.sid == userid)
       //    {
       //        isSelected = true;
       //    }
       //}
       return isSelected;
   }
}

